﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using ZLCMS.Business.Util;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class VoteStatList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected string _userAccount;
        protected WxVoteConfig _voteDb;
        protected int _totalCount;

        private int _uid;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = 4 };

            BindList();
        }

        private void BindList()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                _voteDb = session.CreateCriteria<WxVoteConfig>().Add(Expression.Eq("Id", _uid)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxVoteConfig>();

                if (_voteDb == null)
                {
                    return;
                }

                _totalCount = Convert.ToInt32(session.CreateCriteria<WxVoteLog>().Add(Expression.Eq("OwnerAccount", _userAccount)).Add(Expression.Eq("VoteId", _uid)).SetProjection(Projections.RowCount()).UniqueResult());
              

                Repeater1.DataSource = session.CreateQuery("from WxVoteItem where ConfigId=:cid")
                    .SetParameter("cid", _voteDb.Id)
                    .List();
            }
            Repeater1.DataBind();
        }

        protected int GetVoteCounter(object item)
        {
            int result = 0;
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                result = Convert.ToInt32(session.CreateSQLQuery("select count(*) from wx_vote_log where vote_id=:vid and vote_item=:item and owner_account=:acc")
                    .SetParameter("vid", _uid)
                    .SetParameter("item", item.ToString())
                    .SetParameter("acc", _userAccount)
                    .UniqueResult());
            }
            return result;
        }

        private void InitializeGlobalValue()
        {
            if (!IsPostBack)
            {
                ViewState["uid"] = Request["uid"];
            }

            if (!int.TryParse((string)ViewState["uid"], out _uid))
            {
            }
        }
    }
}
